package com.frame.work.frame.deal;

import com.alibaba.fastjson.JSON;
import com.frame.work.common.constant.RedisKey;
import com.frame.work.mqtt.Topic;
import com.frame.work.mqtt.data.handle.MqttDataHandle;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
@Component
public class Device1018FrameDeal {
    @Autowired
    private MqttDataHandle mqttDataHandle;

    @Autowired
    private RedisTemplate redisTemplate;

    public void deal(String gatewayMac, String data) {
        //设备的网关地址
        String zigbeeMac = StringUtils.substring(data, 2, 18);
        String dataType = StringUtils.substring(data, 0, 2);
        String state = StringUtils.substring(data, data.length() - 2, data.length());
        String childOd = StringUtils.substring(data, data.length() - 4, data.length() - 2);
        if (StringUtils.equals("00", state)) {
            redisTemplate.opsForValue().set(RedisKey.fingerprintLockPsd(zigbeeMac), state, 15, TimeUnit.SECONDS);
        }
        Map<String, Object> map = new HashMap<>(5);
        map.put("type", "lock/psd/key");
        map.put("zigbeeMac", zigbeeMac);
        //写回复命令。02为写成功回复；82为写失败回复
        map.put("dataType", dataType);
        //命令回复。00为成功，其他为失败。
        map.put("state", state);
        //0C密匙 06一次性密码和时效性密码 08 密码错误
        map.put("childOd", childOd);
        mqttDataHandle.mqttPush(Topic.device.topics + gatewayMac + "/lock/psd/key", JSON.toJSONString(map));
    }
}
